<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sapi Extends CI_Controller{
	
	function Sapi()
	{
		parent::__construct();
		$this->load->model('sapi_model');
		$this->load->model('Home_model');
	}
	
	function index()
	{
		if ($this->session->userdata('login') == FALSE){
			redirect('auth');
		}else{
			$id_role = $this->session->userdata('id_role'); 
			$data = array(
				"main_view" 	=> "sapi/konten",
				"title"			=> "Manajemen Sapi",
				"nama_user"		=> "Admin",
				"jenis_sapi"	=> $this->sapi_model->get_jenis_sapi(),
				"daging_sapi"	=> $this->sapi_model->get_daging_sapi(),
				"hak_akses0"	=> $this->Home_model->get_hak_akses($id_role,0),
				"hak_akses1"	=> $this->Home_model->get_hak_akses($id_role,1),
				"hak_akses2"	=> $this->Home_model->get_hak_akses($id_role,2)
			);
			
			$this->load->view('template', $data);
		}
		
	}
	
	function tambah_jenis_sapi()
	{
		$id_sapi = $this->sapi_model->tambah_sapi($this->input->post("nama-jenis"));
		
		if($id_sapi){
			$jenis_sapi = array(
				"ID_SAPI"				=> $id_sapi,
				"NAMA"					=> $this->input->post("nama-jenis"),
				"BERAT_NORMAL"			=> $this->input->post("berat-normal"),
				"PANJANG_NORMAL"		=> $this->input->post("panjang-normal"),
				"TINGGI_NORMAL"			=> $this->input->post("tinggi-normal"),
				"PRODUKSI_DAGING_NORMAL"=> $this->input->post("daging-normal")
			);
			if($this->sapi_model->tambah_jenis_sapi($jenis_sapi)){
				redirect("/sapi");
			} else {
				echo "gagal";
			}
		}
	}
	
	function tambah_daging_sapi()
	{
		$id_sapi = $this->sapi_model->tambah_sapi($this->input->post("nama-daging"));
		if($id_sapi){
			$daging_sapi = array(
				"ID_SAPI"		=> $id_sapi,
				"ID_DAGING_SAPI"=> $id_sapi,
				"NAMA"			=> $this->input->post("nama-daging"),
				"INFO_DAGING"	=> $this->input->post("info-daging")
			);
			if($this->sapi_model->tambah_daging_sapi($daging_sapi)){
				redirect("/sapi");
			} else {
				echo "gagal";
			}
		}
	}
	
	function hapus_jenis_sapi()
	{
		$id_sapi = $this->input->post("id-sapi");
		if($this->sapi_model->hapus_jenis_sapi($id_sapi)){
			if($this->sapi_model->hapus_sapi($id_sapi)){
				echo json_encode(true);
			} else {
				echo json_encode(false);
			}
		}
	}
	
	function hapus_daging_sapi()
	{
		$id_sapi = $this->input->post("id-sapi");
		if($this->sapi_model->hapus_daging_sapi($id_sapi)){
			if($this->sapi_model->hapus_sapi($id_sapi)){
				echo json_encode(true);
			} else {
				echo json_encode(false);
			}
		}
	}
	
	function ubah_jenis_sapi()
	{
		$nama = $this->input->post("nama-jenis");
		$id = $this->input->post("id-sapi");
		if($this->sapi_model->ubah_sapi($id, $nama)){
			$jenis_sapi = array(
				"NAMA"			=> $nama,
				"BERAT_NORMAL"	=> $this->input->post("berat-normal"),
				"PANJANG_NORMAL"=> $this->input->post("panjang-normal"),
				"TINGGI_NORMAL"	=> $this->input->post("tinggi-normal")
			);
			if($this->sapi_model->ubah_jenis_sapi($id, $jenis_sapi)){
				redirect("/sapi");
			} else {
				echo "gagal";
			}
		}
	}
	function ubah_daging_sapi()
	{
		$nama = $this->input->post("nama-daging");
		$id = $this->input->post("id-sapi");
		if($this->sapi_model->ubah_sapi($id, $nama)){
			$daging_sapi = array(
				"NAMA"			=> $nama,
				"INFO_DAGING"	=> $this->input->post("info-daging")
			);
			if($this->sapi_model->ubah_daging_sapi($id, $daging_sapi)){
				redirect("/sapi");
			} else {
				echo "gagal";
			}
		}
	}
	
	/*function tambah_sapi()
	{
		$tanggal = explode("/",$this->input->post("tanggal"));
		$tanggal = $tanggal[2]."-".$tanggal[0]."-".$tanggal[1];
		$data = array(
			"ID_USER" 			=> $this->session->userdata('id_user'),
			"ID_SAPI"			=> $this->input->post("jenis_sapi"),
			"DARI"				=> $this->input->post("dari_titik_pasok"),
			"KE"				=> $this->input->post("ke_titik_pasok"),
			"ID_SATUAN"			=> "3",
			"JUMLAH"			=> $this->input->post("jumlah"),
			"TANGGAL_KEJADIAN"	=> $tanggal,
			"KOMENTAR"			=> $this->input->post("komentar")
		);
		$aksi = $this->input->post("aksi");
		
		if($this->sapi_model->insert_perubahan_stok($data, $aksi))
		{
			redirect("/sapi");
			
		}else{
			echo "gagal";
		}
	}
	*/
	
}